﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace 原神曲目生成器
{
    public class Player
    {
        [DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
        public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
        public Player()
        {

        }
        List<string> MusicKey = new List<string>();
        int MusicStep = 1;
        bool mPlay = false;
        /// <summary>
        /// 是否在播放
        /// </summary>
        public bool IsPlay { get => mPlay; }
        public void Play(string path)
        {
            if (mPlay)
            {
                mPlay = false;
                Thread.Sleep(500);
            }
            mPlay = true;
            MusicKey.Clear();
            //读取文件
            // 创建一个 StreamReader 的实例来读取文件 
            // using 语句也能关闭 StreamReader
            using (StreamReader sr = new StreamReader(path))
            {
                List<string> lines = new List<string>();
                string line;
                do
                {
                    line = sr.ReadLine();
                    lines.Add(line);
                } while (line != null);
                //解析
                MusicStep = int.Parse(lines[0]);
                for (int i = 1; i < lines.Count; i++)
                {
                    string line_ = lines[i];
                    if (line_ == null) break;
                    bool Mclick = false;
                    string Mstr = "";
                    foreach (var c in line_)
                    {
                        if (c == '(')
                        {
                            Mclick = true;
                            Mstr = "";
                        }
                        else if (c == ')')
                        {
                            Mclick = false;
                            MusicKey.Add(Mstr);
                        }
                        else if (c == '-')
                        {
                            MusicKey.Add("");
                        }
                        else if ('A' <= c && c <= 'Z')
                        {
                            if (Mclick)
                            {
                                Mstr += c;
                            }
                            else
                            {
                                MusicKey.Add(c + "");
                            }
                        }

                    }
                }
                //解析完成
                new Thread(() =>
                {
                    int sleepSet =  1000 * 60/ MusicStep/2;
                    foreach (var item in MusicKey)
                    {
                        if (!mPlay) return;
                        //输出按键
                        // SendKeys.SendWait(item);
                      
                        foreach (var it in item)
                        {
                            keybd_event((Keys)it, 0, 0, 0);                           
                        }
                        Thread.Sleep(10);
                        foreach (var it in item)
                        {                            
                            keybd_event((Keys)it, 0, 2, 0);                         
                        }
                        Thread.Sleep(sleepSet-10);
                    }
                    mPlay = false;
                })
                { IsBackground = true }.Start();

            }
        }
        public void Stop()
        {
            mPlay = false;
        }
    }
}
